﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EC_Agent.AgentClass;
using EC_Agent.Models;
using EC_Agent;

namespace EC_Agent
{
    public partial class LoaiDV : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillLoaiDVGrid();
                //abc.InnerHtml = "<br/><h1>Ánh Minh</h1>";
            }
        }

        private void FillLoaiDVGrid()
        {
            gvAGENT.DataSource = new LoaiDVModels().ListLoaiDV();
            gvAGENT.DataBind();
        }

        protected void gvAGENT_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvAGENT.EditIndex = -1;
            FillLoaiDVGrid();
        }

        protected void gvAGENT_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Int16 ID = Convert.ToInt16(gvAGENT.DataKeys[e.RowIndex].Values[0].ToString());
            LoaiDVInfo loaidv = new LoaiDVInfo();
            loaidv.MaLoaiDV = ID;
            new LoaiDVModels().DeleteLoaiDV(loaidv);
            FillLoaiDVGrid();
        }

        protected void gvAGENT_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                LoaiDVInfo eInfo = new LoaiDVInfo();
                eInfo.TenLoaiDV = ((TextBox)gvAGENT.FooterRow.FindControl("txtTenLoaiDV")).Text;
                new LoaiDVModels().InsertLoaiDV(eInfo);
                FillLoaiDVGrid();
            }
        }

        protected void gvAGENT_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvAGENT.EditIndex = e.NewEditIndex;
            FillLoaiDVGrid();
        }

        protected void gvAGENT_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            LoaiDVInfo eInfo = new LoaiDVInfo();
            eInfo.MaLoaiDV = Convert.ToInt16(gvAGENT.DataKeys[e.RowIndex].Values[0].ToString());
            eInfo.TenLoaiDV = ((TextBox)gvAGENT.Rows[e.RowIndex].FindControl("txtTenLoaiDV")).Text;
            new LoaiDVModels().UpdateLoaiDV(eInfo);
            gvAGENT.EditIndex = -1;
            FillLoaiDVGrid();
        }

        protected void gvAGENT_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            /*if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
                if (ddlDepartment != null)
                {
                    ddlDepartment.DataSource = new mainSQL().getDepartmentList();
                    ddlDepartment.DataBind();
                    ddlDepartment.SelectedValue = gvEG.DataKeys[e.Row.RowIndex].Values[1].ToString();
                }
            }
            if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            {
                DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
                if (ddlDepartment != null)
                {
                    ddlDepartment.DataSource = new mainSQL().getDepartmentList();
                    ddlDepartment.DataBind();
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
                ddlDepartment.DataSource = new mainSQL().getDepartmentList(); ;
                ddlDepartment.DataBind();
            }*/
        }
    }
}